﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Sig2Code : UserControl
    {
        public Sig2Code()
        {
            InitializeComponent();


            richTextBox1.Text = @"E8 ? ? ? ? 48 8B 4D FF 48 33 CC E8 ? ? ? ? 48 81 C4 F0 00 00 00 5D C3";
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void buttonConverter_Click(object sender, EventArgs e)
        {
            List<ushort> list = new List<ushort>();


            var txt = richTextBox1.Text.Replace(" ","");

            StringReader sr = new StringReader(txt );

            char[] buf ;
            buf  = new char[2];
            if( ! richTextBox1.Text.Contains("??"))
            {

                while (sr.Read(buf, 0, 2) == 2)
                {
                    var str = new string(buf);
                    if (str.Contains("?"))
                    {
                        list.Add(0x100);
                        list.Add(0x100);
                    }
                    else
                    {
                        list.Add(Convert.ToUInt16(str.Trim(), 16));
                    }
                }
            }else
            {
                while (sr.Read(buf, 0, 2) == 2)
                {
                    var str = new string(buf);
                    if (str.Contains("?"))
                        list.Add(0x100);
                    else
                    {
                       list.Add(Convert.ToUInt16(str.Trim(), 16));
                    }
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("unsigned short buffer[0x" + list.Count.ToString("X") + "] = {\n");

            int lines = list.Count / 16;
            int others = list.Count % 16;

            for (int y = 0; y <= lines; y++)
            {
                //行数遍历  
                sb.Append("\t");
                for (int x = 0; y == lines ? x < others : x < 16; x++)
                {
                    sb.Append("0x");
                    sb.Append(list[y * 16 + x].ToString("X"));
                    sb.Append(",");
                }
                sb.Append("\n");

            }
            sb.Append("\n};");


            richTextBox2.Text = sb.ToString();
        }

        private void buttonCopy_Click(object sender, EventArgs e)
        {

        }

    }
}
